package com.hotel.po.controller;

import com.github.pagehelper.PageInfo;
import com.hotel.common.utils.Result;
import com.hotel.po.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/po/reviews")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    @GetMapping
    public Result<java.util.Map<String, Object>> list(@RequestParam(name = "hotelId", required = false) Long hotelId,
                                                      @RequestParam(name = "page", defaultValue = "1") int page,
                                                      @RequestParam(name = "size", defaultValue = "10") int size) {
        PageInfo<java.util.Map<String, String>> pageInfo = reviewService.list(hotelId, page, size);
        java.util.HashMap<String, Object> resp = new java.util.HashMap<>();
        resp.put("list", pageInfo.getList());
        resp.put("total", pageInfo.getTotal());
        return Result.success(resp);
    }

    @GetMapping("/all")
    public Result<java.util.Map<String, Object>> listAll(@RequestParam(name = "page", defaultValue = "1") int page,
                                                         @RequestParam(name = "size", defaultValue = "10") int size) {
        PageInfo<java.util.Map<String, String>> pageInfo = reviewService.listAll(page, size);
        java.util.HashMap<String, Object> resp = new java.util.HashMap<>();
        resp.put("list", pageInfo.getList());
        resp.put("total", pageInfo.getTotal());
        return Result.success(resp);
    }

    @PostMapping
    public Result<Long> create(@RequestBody Map<String, Object> body,
                               @RequestHeader(value = "X-User-Id", required = false) String userIdHeader) {
        if (userIdHeader == null || userIdHeader.isEmpty()) {
            return Result.error("未登录");
        }
        Long userId = Long.valueOf(userIdHeader);
        Long hotelId = body.get("hotelId") == null ? null : Long.valueOf(body.get("hotelId").toString());
        Integer rating = body.get("rating") == null ? null : Integer.valueOf(body.get("rating").toString());
        String content = body.get("content") == null ? null : body.get("content").toString();
        Long id = reviewService.create(hotelId, userId, rating, content);
        return Result.success(id);
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable("id") Long id) {
        reviewService.delete(id);
        return Result.success();
    }
}


